package com.traffic.controller;



import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.traffic.annotation.ApiJsonObject;
import com.traffic.annotation.ApiJsonProperty;
import com.traffic.constant.Status;

import com.traffic.entity.Guest;
import com.traffic.entity.Rank;
import com.traffic.model.ApiResponse;


import com.traffic.model.LevelResult;
import com.traffic.service.FriendService;
import com.traffic.service.GuestService;
import com.traffic.service.RankService;


import com.traffic.util.CalculateUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Api(tags = "驾驶行为总数居")
@RestController
@RequestMapping(value = "/rank")
@Slf4j

public class RankController {

    @Autowired
    private RankService rankService;

    @Autowired
    private FriendService friendService;

    @Autowired
    private CalculateUtil calculateUtil;

    @Autowired
    private GuestService guestService;

    @ApiOperation(value = "【个人驾驶行为数据】", notes = "驾驶数据个人")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "user_id", value = "用户ID", required = true)
    @GetMapping("/getData/{user_id}")
    public ApiResponse calculateRisk( @PathVariable Long user_id) {

        Rank individual = rankService.selectByUserId(user_id);

        QueryWrapper<Guest> wrapper = new QueryWrapper<>();

        Guest oguest = guestService.getOne(wrapper.last("WHERE TO_DAYS(create_time) = TO_DAYS(NOW())"));
        if(oguest == null){
            Guest guest = new Guest();
            guest.setNumber(new Long(1));
            guest.setCreateTime(new DateTime());
            guestService.save(guest);
        }
        else{
            oguest.setNumber(oguest.getNumber()+1);
            guestService.updateById(oguest);
        }

        return ApiResponse.ofStatus(Status.OK,individual);

    }

    @ApiOperation(value = "【排行榜是否为好友】", notes = "排行榜个人是否为好友")
    @PostMapping("/isfriend")
    @ApiJsonObject(name = "myrank",value = {
            @ApiJsonProperty(type =String.class,key = "user_id",example = "1" , description = "用户id"),
            @ApiJsonProperty(type =String.class,key = "target_id",example = "2" , description = "对方id"),

    })
    public ApiResponse rankIsFriend(@RequestBody Map<String, String> param) {

        Long userid = new Long(param.get("user_id"));
        Long targetid = new Long(param.get("target_id"));

        Integer isfriend = friendService.isFriend(userid,targetid);

        return ApiResponse.ofStatus(Status.OK,isfriend);

    }


    @ApiOperation(value = "【个人驾驶里程】", notes = "驾驶总里程和等级")
    @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "user_id", value = "用户ID", required = true)
    @GetMapping("/getmileage/{user_id}")
    public ApiResponse getMileage( @PathVariable Long user_id) {

        QueryWrapper<Rank> wrapper = new QueryWrapper<>();
        Rank rank = rankService.selectByUserId(user_id);

        Integer level = calculateUtil.CalculateLevel(rank.getHisMileage());
        Integer nextlevel = calculateUtil.NextLevel(level+1);

        LevelResult l = new LevelResult();
        l.setLevel(level);
        l.setMileage(rank.getHisMileage().intValue());
        l.setNext(nextlevel);
        l.setScore(rank.getScore());


        return ApiResponse.ofStatus(Status.OK,l);

    }


}
